package com.sh.service;

import com.google.gson.Gson;
import com.sh.bean.*;
import com.sh.utils.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

@Component
@Slf4j
public class JmsMessageConsumer {
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private MailBiz mailBiz;
    @Autowired
    private VelocityTemplateBizImpl velocityTemplateBiz;


    @JmsListener(destination = "TakeawayMessages") // 监听bankMessages消息队列
    public void receiveMessage(String message) {
        Gson gson = new Gson();
        MessageBean messageBean = gson.fromJson(message, MessageBean.class);
        System.out.println("从消息队列里面取的消息：" + message);
//        mailBiz.send(messageBean.toString());
        String content = velocityTemplateBiz.genEmailContent(
                messageBean.getUsername(),
                messageBean.getCart(),
                messageBean.getUserid()
                );
        log.info("发送邮件内容:{}", content);
        mailBiz.send(content);
        MessageBeanHtml messageBeanHtml = new MessageBeanHtml();
//        BeanUtils.copyProperties(messageBean, messageBeanHtml);
        messageBeanHtml.setConsume(messageBean.getUsername());
        Map<String, CartItem> cartItemMap = messageBean.getCart();
        Collection<CartItem> values = cartItemMap.values();
        StringBuffer fnameList = new StringBuffer();
        for (CartItem value : values) {
            String temp = value.getFood().getFname();
            fnameList.append(temp).append(" ");
        }
        messageBeanHtml.setDishList(fnameList.toString());
        Date date = new Date();
        // 设置时区为中国（东八区）
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 格式化输出
        String formattedDate = sdf.format(date);
        messageBeanHtml.setSendTime(formattedDate);
        String json = gson.toJson(messageBeanHtml);
        webSocket.sendMessageToAllClients(json);

    }


    @JmsListener(destination = "TelMessages") // 监听bankMessages消息队列
    public void sendTelVerify(String message) {
        Gson gson = new Gson();
        TelBean telBean = gson.fromJson(message, TelBean.class);
        System.out.println("要发送的电话 ：  ============" + telBean.getTel() );
        Date date = new Date();
        // 设置时区为中国（东八区）
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        // 格式化输出
        String formattedDate = sdf.format(date);
        TelBeanHtml telBeanHtml = new TelBeanHtml();
        telBeanHtml.setTel(telBean.getTel());
        telBeanHtml.setSendTelTime(formattedDate);
        String json = gson.toJson(telBeanHtml);
        webSocket.sendMessageToAllClients(json);
    }




}
